package zhoufu.sz.sanxiao_check.ui.fragment;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.cy.src.update.download.Config;
import com.cy.src.update.ui.UpdateTask;
import java.io.File;
import java.util.UUID;
import javax.inject.Inject;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.config.Constant;
import zhoufu.sz.sanxiao_check.databinding.FragmentLoginBinding;
import zhoufu.sz.sanxiao_check.inject.components.LoginComponet;
import zhoufu.sz.sanxiao_check.models.User;
import zhoufu.sz.sanxiao_check.navigation.Navigator;
import zhoufu.sz.sanxiao_check.presents.LoginPresenter;
import zhoufu.sz.sanxiao_check.services.DownloadService;
import zhoufu.sz.sanxiao_check.ui.contract.LoginContract;
import zhoufu.sz.sanxiao_check.utils.UIUtils;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/7/19
 */
public class LoginFragment extends BaseFragment implements LoginContract.View
{
    private FragmentLoginBinding _loginBinding;

    @Inject
    LoginPresenter _presenter;

    @Inject
    Navigator _navigator;


    @Nullable @Override public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        update();
        _loginBinding = DataBindingUtil
                .inflate(inflater, R.layout.fragment_login, container, false);
        LoginComponet componet = getComponent(LoginComponet.class);
        LoginComponet.Instance.init(componet);
        componet.inject(this);
        _presenter.bindV(this);
        _loginBinding.setPresenter(_presenter);
        return _loginBinding.getRoot();
    }

    private void update()
    {
        Config config = new Config();
        config.setUpdateUrl(Constant.UPDATE_URL);
        config.setSilenceUpdate(true);//是否静默请求更新
        File file = new File(Environment.getExternalStorageDirectory(),
                mAct.getPackageName() + ".apk");
        config.setLocalDownloadFile(file);
        new UpdateTask(mAct, config).startRequest();
    }

    /**
     * 检查 并更新
     */
    private void upgradeApp()
    {
        String apkInfoAdd = "http://120.31.131.105:8002/apk/version_sanxiao.xml";
        DownloadService.startDownLoadApk(getActivity(), apkInfoAdd);
    }

    private String uuid()
    {
        String uuid = mAct.getPreferences(Context.MODE_PRIVATE).getString(PRE_UUID, null);
        if ( uuid == null )
        {
            uuid = UUID.randomUUID().toString();
            mAct.getPreferences(Context.MODE_PRIVATE).edit().putString(PRE_UUID, uuid).commit();
        }
        return uuid;
    }

    @Override public void onStart()
    {
        super.onStart();
        _presenter.onStart();
    }

    @Override public void showProgress()
    {
        showProgreesDialog();
    }

    @Override public void hideProgress()
    {
        hideProgreesDialog();
    }

    @Override public void setUser(User user)
    {
        _loginBinding.setUser(user == null ? new User() : user);
    }

    @Override public void showEmpty()
    {

    }

    @Override public String getUUid()
    {
        return uuid();
    }

    public String getVersionName()
    {
        PackageInfo mPackageInfo = null;
        try
        {
            mPackageInfo = mAct.getPackageManager()
                    .getPackageInfo(mAct.getPackageName(), 0);// getPackageName()是你当前类的包名，0代表是获取版本信息
            return mPackageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e)
        {
            e.printStackTrace();
        }
        return "0";
    }

    @Override public void loginSuccessed()
    {
        mAct.finish();
        _navigator.startMainAct(mAct);
    }

    @Override public void loginFaild(String errMsg)
    {
        UIUtils.showSnackbar(_loginBinding.getRoot(), errMsg);
    }

    @Override public boolean checkEmpty()
    {
        return false;
    }

    public static final String PRE_UUID = "pre_uuid";

    @Override public void showToast(String msg)
    {
        UIUtils.showSnackbar(getView(), msg);
    }

    @Override public void pullFaild()
    {
        UIUtils.showSnackbar(_loginBinding.getRoot(), R.string.toast_network_error);
    }

    @Override public void pullEnd()
    {

    }

    @Override public void pullStart()
    {

    }

    @Override public void onDestroy()
    {
        super.onDestroy();
        _presenter.onDestroy();
    }
}
